const Sequelize = require('sequelize')
const {sequelize} = require('@/mysql/utils')
const {Project} = require('@/mysql/project')
const {User} = require('@/mysql/user')
// 创建用户实体
const File = sequelize.define('file', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: Sequelize.STRING,
            allowNull: false
        },
        path: {
            type: Sequelize.STRING
        },
        type: {
            type: Sequelize.STRING
        },
        size: {
            type: Sequelize.INTEGER,
            defaultValue: 0
        },
        create_user: {
            type: Sequelize.STRING
        },
        deleted: {
            type: Sequelize.BOOLEAN,
            defaultValue: false
        }
    }
);
File.belongsTo(Project)
File.belongsTo(User)
File.sync({force: false})
/**插入新文件数据（添加） */
File.insertData = async (name, path, type, size, project_id) => {
    await File.sync({
        force: false
    })
    // 表已创建
    return File.create({name, path, type, size, projectProjectId: project_id, userId: global.utils.getUserId()})
}
File.insertImage = async (name, path, type, size) => {
    await File.sync({
        force: false
    })
    // 表已创建
    return File.create({name, path, type, size})
}
// 查找文件分类类型
File.findById = async (id) => {
    return File.findAll({where: {id}})
}
File.getAllFile = async (projectProjectId, deleted = false) => {
    return File.findAll({
        where: {projectProjectId, deleted}, include: [{
            model: User,
            attributes: ['name'],
            where: {id: Sequelize.col('userId')}
        }]
    })

}
export {File}

